/* GUI CONVERTED from ecincsel.p (converter v1.76) Tue Dec 31 10:45:12 2002 */
/* ecincsel.p - PCO INCORPORATION SELECTION                             */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Revision: 1.12.1.8 $                                                     */
/*V8:ConvertMode=Maintenance                                            */
/* REVISION: 7.4      LAST EDIT: 10/12/93   MODIFIED BY: pma *H013*     */
/*                               10/27/94   MODIFIED BY: ame *H578*     */
/*                               04/24/95            by: srk *H0D8*     */
/*                               07/13/95   MODIFIED BY: ais *H0FB*     */
/*                               01/29/96   MODIFIED BY: bcm *H0JH*     */
/*                               02/16/96   MODIFIED BY: rvw *H0JT*     */
/* REVISION: 8.6     LAST EDIT: 12/09/96    MODIFIED BY: Joe Gawel  *K00C**/
/*K00C* - Replaced ecm__qad0* fields with appropriate new fields
 *      - No longer need to call gpdatdec.p to convert ecm__qad05 field
 *          since new ecm_selected_eff field replaces ecm__qad05
 *      - No longer need to call ecrevchk.p to check revision levels
*K00C*/
/* REVISION: 8.6     LAST EDIT: 01/30/97    MODIFIED BY: Kieu Nguyen *K05D* */
/* REVISION: 8.6     LAST MODIFIED: 07/01/97  BY: *H0ZS* Felcy D'Souza      */
/* REVISION: 8.6     LAST EDIT: 07/14/97    MODIFIED BY: Joe Gawel *K0GG*   */
/* REVISION: 8.6     LAST EDIT: 09/26/97    MODIFIED BY: Jean Miller *K0GG* */
/* REVISION: 8.6     LAST EDIT: 10/07/97    MODIFIED BY: John Worden *K0LP* */
/* REVISION: 8.6     LAST EDIT: 10/17/97    MODIFIED BY: John Worden *K12D* */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane        */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan       */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan       */
/* REVISION: 8.6E     LAST MODIFIED: 12/10/98   BY: *J36N* Santhosh Nair    */
/* REVISION: 8.6E     LAST MODIFIED: 03/21/00   BY: *L0V4* Jyoti Thatte     */
/* REVISION: 9.1      LAST MODIFIED: 07/11/00   BY: *N0FJ* Mudit Mehta      */
/* REVISION: 9.1      LAST MODIFIED: 02/07/01   BY: *M11C* Vandna Rohira    */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00   BY: *N0KW* Jacolyn Neder    */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Old ECO marker removed, but no ECO header exists *K0K4*                    */
/* $Revision: 1.12.1.8 $    BY: Katie Hilbert        DATE: 12/27/02  ECO: *P0L8*   */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdtitle.i "2+ "}

define variable nbr           like ecm_nbr label "Number".
define variable ecmnbr        like nbr.
define variable ecmid         like wo_lot label "PCO ID".
define variable dbase         like si_db.
define variable ecmnbr1       like nbr.
define variable ecmid1        like wo_lot label "PCO ID".
define variable dbase1        like si_db.
define variable dte_out       as date.
define variable dec_out       as integer.
define variable ecmtype       like ecm_type.
define variable ecminit       like ecm_init label "Created By".
define variable yn            like mfc_logical.
define variable error-msg-nbr like msg_nbr no-undo.
define variable un_only       like mfc_logical initial yes
                              label "Unincorporated Only".

define buffer ecd1det for ecd1_det.
define buffer ecd2det for ecd2_det.
define buffer ecd4det for ecd4_det.
define buffer ecd for ecd_det.

define temp-table wrk_que
   field ecm__key   like ecm_nbr
   field ecm__recid as recid
   field ecm__nbr   like ecm_nbr column-label "Number"
   field ecm__id    like ecmid
   field ecm__dbase like dbase
   field ecm__title like ecm_title
   field ecm__eff   like ecm_eff_date
   field effdate    like ecm_eff_date initial ?
   field incp_yn    as character format "x(1)".

{ecwndvar.i}  /*scrolling window variables*/

/*TOP FRAME*/
FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
ecmnbr               colon 13
   ecmid                colon 35
   dbase                colon 67
   ecmtype              colon 13
   ecminit              colon 35
   un_only              colon 67
 SKIP(.4)  /*GUI*/
with frame aa  width 80 side-labels NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-aa-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame aa = F-aa-title.
 RECT-FRAME-LABEL:HIDDEN in frame aa = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame aa =
  FRAME aa:HEIGHT-PIXELS - RECT-FRAME:Y in frame aa - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME aa = FRAME aa:WIDTH-CHARS - .5.  /*GUI*/


/* SET EXTERNAL LABELS */
setFrameLabels(frame aa:handle).

/*BOTTOM FRAME*/

/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   nbr
   ecm__id
   ecm__dbase
   ecm__title
   effdate
   incp_yn no-label
with frame a width 80 attr-space THREE-D /*GUI*/.

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

/* DEFINED FRAME W2 FOR GUI INTERFACE */
FORM /*GUI*/ 
   ecm__nbr
   ecm__id
   ecm__dbase
   ecm__title
   effdate
   incp_yn no-label
with frame w2 row window_row scroll 1 window_down down overlay
   no-validate width 80 attr-space title color normal "" BGCOLOR 8 /*GUI*/.


/* SET EXTERNAL LABELS */
setFrameLabels(frame w2:handle).
/* END FRAME DEFINITION */

assign
   window_row = 6
   window_down = 5.

view frame aa.

find first pcc_ctrl no-lock no-error.
if (available pcc_ctrl and pcc_ecooff_date = ?)
   or not available pcc_ctrl then do:
   /* ECO Maintenance on Product Structure Menu is not disabled */
   {pxmsg.i &MSGNUM=5652 &ERRORLEVEL=4}
   pause.
   hide message no-pause.
   return.
end.

loop-a:
repeat with frame aa on endkey undo, leave:
/*GUI*/ if global-beam-me-up then undo, leave.

   clear frame w2 all no-pause.
   clear frame a all no-pause.
   view frame aa.
   view frame w2.
   view frame a.
   release ecm_mstr.
   first_time = yes.

   global_recid = ?.
   update
      ecmnbr
      ecmid
      dbase
      ecmtype
      ecminit
      un_only
   with frame aa
   editing:
      if frame-field = "ecmnbr" or frame-field = "ecmid"
         or frame-field = "dbase" then do:
         /* FIND NEXT/PREV*/
         {mfnp05.i ecm_mstr ecm_mstr
            "((ecm_rel_date <> ?) and (ecm_close_date = ?))"
            ecm_nbr ecmnbr}

         if global_recid <> ? then do:
            recno = global_recid.
            find ecm_mstr where recid(ecm_mstr) = recno.
            global_recid = ?.
         end.

         if recno <> ? then
            display
               substring(ecm_nbr,1,8)  @ ecmnbr
               substring(ecm_nbr,9,8)  @ ecmid
               substring(ecm_nbr,17,8) @ dbase
               ecm_type                @ ecmtype
               ecm_init                @ ecminit
            with frame aa.
      end.
      else do:
         readkey.
         apply lastkey.
      end.
   end.
   global_recid = ?.

   run create-wrk-que.

   global_recid = ?.

   loop-b:
   repeat with frame a:
/*GUI*/ if global-beam-me-up then undo, leave.

      clear frame w2 all no-pause.
      clear frame a all no-pause.
      view frame w2.
      view frame a.
      release ecm_mstr.
      assign
         ecmnbr1 = ""
         ecmid1  = ""
         dbase1  = "".

      {ecwindow.i wrk_que
         "ecm__nbr ecm__id ecm__dbase
           ecm__title effdate incp_yn no-label"
         ecm__nbr
         "/**/"
         yes
         """"
         "width 80 attr-space"
         "2"
         "ecm__nbr ecm__id ecm__dbase"
         }

      if lastkey = keycode("GO") or
         lastkey = keycode("F1") or
         lastkey = 301 or keyfunction(lastkey) = "GO" or
         lastkey = keycode("RETURN")
      then do:
         frame-value = ix1array[i].
         global_recid = recidarray[i].
      end.
      else do:
         frame-value = frame_val_save.
         global_recid = ?.
         if keyfunction(lastkey) = "end-error" or
            keyfunction(lastkey) = "endkey"    or
            lastkey = keycode("F4")
         then leave.
         else apply lastkey.
         next.
      end.

      loop-c:
      do transaction with frame a on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.

         release ecm_mstr.
         find first wrk_que where recid(wrk_que) = global_recid
            no-error.
         if available wrk_que then
            display
               ecm__nbr @ nbr
               ecm__id
               ecm__dbase
               ecm__title
               effdate
               incp_yn
            with frame a.

         recno = global_recid.
         set nbr with frame a
         editing:
            {mfnp10.i wrk_que nbr ecm__nbr nbr ecm__nbr}
            if recno <> ? then do:
               display
                  ecm__nbr @ nbr
                  ecm__id
                  ecm__dbase
                  ecm__title
                  effdate
               with frame a.
               global_recid = recno.
            end.
         end.

         assign
            ecmnbr1 = nbr
            ecmid1 = ecm__id
            dbase1 = ecm__dbase.

         find first wrk_que
            where ecm__key begins string(ecmnbr1,"x(8)")
                                + string(ecmid1, "x(8)")
                                + string(dbase1, "x(8)")
         no-error.

         if not available wrk_que then do:

            do on endkey undo, leave:
               /* No record available based on selection criteria */
               {pxmsg.i &MSGNUM=5657 &ERRORLEVEL=3}
               pause.
            end. /* DO ON ENDKEY UNDO, LEAVE */

            undo loop-b, retry.
         end.

         global_recid = recid(wrk_que).

         update effdate with frame a.

         /*CHANGED EFFECTIVE DATE*/
         if ecm__eff <> ? or (effdate <> ? and effdate <> ecm__eff)
         then do:

            for each ecm_mstr no-lock
               where ecm_nbr <> ecm__key
                 and ((effdate          = ecm_eff_date and
                       ecm_selected_eff = ?)
                  or (ecm_selected_eff = effdate and effdate <> ?))
               use-index ecm_eff_date,

               each ecd1_det no-lock where ecd1_nbr = ecm_nbr:
/*GUI*/ if global-beam-me-up then undo, leave.


               if can-find (ecd1det where
                  ecd1det.ecd1_nbr  = ecm__key           and
                  ecd1det.ecd1_par  = ecd1_det.ecd1_par  and
                  ecd1det.ecd1_comp = ecd1_det.ecd1_comp and
                  ecd1det.ecd1_ref  = ecd1_det.ecd1_ref)
               then do:
                  /* EFFECTIVE DATE CONFLICTS WITH PCO: # */
                  {pxmsg.i &MSGNUM=2167 &ERRORLEVEL=4 &MSGARG1=ecm_nbr}
                  next-prompt effdate with frame a.
                  undo loop-c, retry.
               end.
            end.
/*GUI*/ if global-beam-me-up then undo, leave.

            for each ecm_mstr no-lock
               where ecm_nbr <> ecm__key
                 and ((effdate <= ecm_eff_date and ecm_selected_eff = ?)
                  or (ecm_selected_eff = effdate and effdate <> ?))
               use-index ecm_eff_date:
/*GUI*/ if global-beam-me-up then undo, leave.


               for each ecd2_det no-lock where ecd2_nbr = ecm_nbr:
/*GUI*/ if global-beam-me-up then undo, leave.

                  if can-find (ecd2det where
                     ecd2det.ecd2_nbr     = ecm__key              and
                     ecd2det.ecd2_routing = ecd2_det.ecd2_routing and
                     ecd2det.ecd2_op      = ecd2_det.ecd2_op)
                  then do:
                     /* EFFECTIVE DATE CONFLICTS WITH PCO */
                     {pxmsg.i &MSGNUM=2167 &ERRORLEVEL=4 &MSGARG1=ecm_nbr}
                     next-prompt effdate with frame a.
                     undo loop-c, retry.
                  end.
               end.
/*GUI*/ if global-beam-me-up then undo, leave.
   /* FOR EACH ECD2_DET */
               for each ecd4_det no-lock where ecd4_nbr = ecm_nbr:
/*GUI*/ if global-beam-me-up then undo, leave.

                  if can-find (ecd4det where
                     ecd4det.ecd4_nbr      = ecm__key              and
                     ecd4det.ecd4_part     = ecd4_det.ecd4_part    and
                     ecd4det.ecd4_routing  = ecd4_det.ecd4_routing and
                     ecd4det.ecd4_op       = ecd4_det.ecd4_op      and
                     ecd4det.ecd4_sequence = ecd4_det.ecd4_sequence)
                  then do:
                     /* EFFECTIVE DATE CONFLICTS WITH PCO */
                     {pxmsg.i &MSGNUM=2167 &ERRORLEVEL=4 &MSGARG1=ecm_nbr}
                     next-prompt effdate with frame a.
                     undo loop-c, retry.
                  end.
               end.
/*GUI*/ if global-beam-me-up then undo, leave.
   /* FOR EACH ECD4_DET */
            end.
/*GUI*/ if global-beam-me-up then undo, leave.
     /* FOR EACH ECM_MSTR */
         end.

         find ecm_mstr no-lock where recid(ecm_mstr) = ecm__recid
            no-error.

         /*ERROR: PCO DOES NOT EXIST*/
         if not available ecm_mstr then do:

            do on endkey undo, leave:
               /* Process not completed */
               {pxmsg.i &MSGNUM=5630 &ERRORLEVEL=3}
               pause.
            end. /* DO ON ENDKEY UNDO, LEAVE */

            undo loop-b, retry.
         end.

         /*ERROR: EFFDATE CANNOT BE < TODAY*/
         if (ecm__eff = ? or ecm__eff > today)
            and effdate < today then do:
            /* Effective date cannot be before today */
            {pxmsg.i &MSGNUM=5659 &ERRORLEVEL=3}
            next-prompt effdate with frame a.
            undo loop-c, retry.
         end.

         /*ERROR: OVERDUE RESCHEDULE NOT ALLOWED*/
         if ecm__eff < today and effdate < today
            and ecm__eff <> effdate then do:
            /* Overdue reschedule not allowed */
            {pxmsg.i &MSGNUM=5660 &ERRORLEVEL=3}
            next-prompt effdate with frame a.
            undo loop-c, retry.
         end.

         /*PCO EFFECTIVE DATE WILL BE CHANGED.  CONTINUE*/
         if ecm__eff <> ? and effdate <> ecm__eff then do:
            yn = yes.
            if effdate = ? then do:
               /* PCO will be unincorporated.  Continue? */
               {pxmsg.i &MSGNUM=2169 &ERRORLEVEL=2 &CONFIRM=yn}
            end.
            else do:
               /* PCO effective date will be changed.  Continue? */
               {pxmsg.i &MSGNUM=2168 &ERRORLEVEL=2 &CONFIRM=yn}
            end.
            if not yn then undo loop-c, retry.
         end.

         find ecm_mstr where recid(ecm_mstr) = ecm__recid
         exclusive-lock no-error no-wait.
         /*PCO LOCKED*/
         if locked ecm_mstr then do:

            do on endkey undo, leave:
               /* Record is locked.  Please try again later */
               {pxmsg.i &MSGNUM=7006 &ERRORLEVEL=4}
               pause.
            end. /* DO ON ENDKEY UNDO, LEAVE */

            undo loop-b, retry.
         end.

         /*PCO DOES NOT EXIST*/
         if not available ecm_mstr then do:

            do on endkey undo, leave:
               /* PCO does not exist */
               {pxmsg.i &MSGNUM=2155 &ERRORLEVEL=3}
               pause.
            end. /* DO ON ENDKEY UNDO, LEAVE */

            undo loop-b, retry.
         end.

         /*EFFECTIVE DATE LATER THAN MANDATORY DATE*/
         if effdate <> ? and (effdate > ecm_man_date) then do:

            do on endkey undo loop-b, retry:
               /* Proposed effective date is later than the mandatory date */
               {pxmsg.i &MSGNUM=5668 &ERRORLEVEL=2}
               pause.
            end. /* DO ON ENDKEY UNDO loop-b, RETRY */

         end.

         /*ITEM REV LEVEL CONFLICT*/
         ecdloop:
         for each ecd_det where ecd_nbr = ecm_nbr:
/*GUI*/ if global-beam-me-up then undo, leave.

            ecd__qad02 = effdate.
            if effdate = ? then next ecdloop.

            find pt_mstr no-lock where pt_part = ecd_part.
            /* REVISION CHECKING NOW DONE BY ECREVCHK.P      */
            {gprun.i ""ecrevchk.p""
               "(input pt_rev,
                 input ecd_new_rev,
                 output error-msg-nbr)"}
/*GUI*/ if global-beam-me-up then undo, leave.


            if error-msg-nbr > 0 then do:

               do on endkey undo, leave:
                  /* Revision level conflict with Item # # */
                  {pxmsg.i &MSGNUM=5686 &ERRORLEVEL=4
                           &MSGARG1=ecd_part
                           &MSGARG2=getTermLabel(""IMPLEMENTED"",15)}

                  pause.
               end. /* DO ON ENDKEY UNDO, LEAVE */

               undo loop-b, retry.
            end.

            for each ecd no-lock where ecd.ecd_part = ecd_det.ecd_part
                  and ecd.ecd__qad02 >= today:
/*GUI*/ if global-beam-me-up then undo, leave.


               /* REVISION CHECKING NOW DONE BY ECREVCHK.P */
               if effdate >= ecd.ecd__qad02 then do:
                  {gprun.i ""ecrevchk.p""
                     "(input ecd.ecd_new_rev,
                       input ecd_det.ecd_new_rev,
                       output error-msg-nbr)"}
/*GUI*/ if global-beam-me-up then undo, leave.

               end.
               else do:
                  {gprun.i ""ecrevchk.p""
                     "(input ecd_det.ecd_new_rev,
                       input ecd.ecd_new_rev,
                       output error-msg-nbr)"}
/*GUI*/ if global-beam-me-up then undo, leave.

               end.

               if error-msg-nbr = 5618 then do:

                  do on endkey undo, leave:
                     /* Revision level conflict with Item # # */
                     {pxmsg.i
                        &MSGNUM = 5686
                        &ERRORLEVEL = 4
                        &MSGARG1 = ecd.ecd_part
                        &MSGARG2 = getTermLabel(""PCO_#"",15)
                        &MSGARG3 = trim(substring(ecd.ecd_nbr,1,8))
                        }

                     pause.
                  end. /* DO ON ENDKEY UNDO, LEAVE */

                  undo loop-c, retry.
               end.

               if keyfunction(lastkey) = "end-error" or
                  keyfunction(lastkey) = "endkey" or
                  lastkey = keycode("F4")
               then do:
                  undo loop-c, retry.
               end.
            end.
/*GUI*/ if global-beam-me-up then undo, leave.
  /*for each ecd*/
            if keyfunction(lastkey) = "end-error" or
               keyfunction(lastkey) = "endkey" or
               lastkey = keycode("F4")
            then do:
               undo loop-c, retry.
            end.
         end.
/*GUI*/ if global-beam-me-up then undo, leave.
  /*for each ecd_det*/

         /*PCO UNINCORPORATED*/
         if effdate = ? and ecm_eff_date <> ? then do:
            ecm_selected_eff = low_date.
            leave loop-c.
         end.

         /*NO CHANGE OR UNDONE CHANGE*/
         if effdate = ecm_eff_date then do:
            ecm_selected_eff = ?.
            leave loop-c.
         end.

         /*NO CHANGE*/
         if effdate = ecm__eff then leave loop-c.

         /*CHANGED*/

         if effdate <> ecm_selected_eff then
            ecm_selected_eff = effdate.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /*loop-c*/
      first_time = no.
      global_recid = ?.
   end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* end of loop-b */
end.
/*GUI*/ if global-beam-me-up then undo, leave.
  /* end of loop-a */

global_recid = ?.

PROCEDURE create-wrk-que:
/*-------------------------------------------------------------------------
Purpose:    Create workfile with appropriate ecm_mstr records
Parameters: none
Notes:      Added this procedure with patch /*K0K4*/
--------------------------------------------------------------------------*/

   for each wrk_que exclusive-lock:
      delete wrk_que.
   end.

   /* ASSIGNED THE EXACT FIELD LENGTH TO DISPLAY APPROPRIATE */
   /* RECORD IN ALL COMBINATION OF SELECTION CRITERIA        */
   assign
      ecmnbr = string(ecmnbr, "x(8)")
      ecmid  = string(ecmid, "x(8)")
      dbase  = string(dbase, "x(8)").

   for each ecm_mstr where
      (substring(ecm_nbr,1,8) >= ecmnbr or ecmnbr = "") and
      (substring(ecm_nbr,9,8) = ecmid  or ecmid  = "")  and
      (substring(ecm_nbr,17,8) = dbase  or dbase  = "") and
      (ecm_type = ecmtype or ecmtype = "") and
      (ecm_init = ecminit or ecminit = "") and
      substring(ecm_doc_type,3) <> "R"     and
      (ecm_rel_date <> ?)  and
      (ecm_close_date = ?) and
      (ecm_end_date = ?)   and
      (ecm_eff_date = ? or (ecm_eff_date <> ? and ecm_eff_date >= today and
       not un_only))
      no-lock by ecm_nbr:

      create wrk_que.
      assign
         ecm__key = string(ecm_nbr,"x(24)")
         ecm__recid = recid(ecm_mstr)
         ecm__nbr = substring(ecm_nbr,1,8)
         ecm__id = substring(ecm_nbr,9,8)
         ecm__dbase = substring(ecm_nbr,17,8)
         ecm__title = ecm_title
         ecm__eff = ecm_eff_date.

      if recid(wrk_que) = -1 then .
      if ecm_eff_date <> ? then incp_yn = "*".

      /***************************************************************/
      /*IF ECM_EFF_DATE <> ? AND ECM_SELECTED_EFF <> ? THEN THE PCO  */
      /*HAS BOTH BEEN INCORPORATED AND QUEUED UP FOR ANOTHER CHANGE  */
      /*IS EFFDATE THE PREVIOUS ACTUAL OR THE QUEUED CHANGE?         */
      /*ANSWER: THE QUEUED CHANGE; SINCE THAT WAS MY LAST SELECTED   */
      /*        DATE.  ECM__EFF IS SET AT THIS DATE.                 */
      /***************************************************************/
      effdate = ecm_eff_date.
      if ecm_selected_eff <> ? then do:
         effdate = ecm_selected_eff.
         if effdate = low_date then effdate = ?.
      end.

      ecm__eff = effdate.

   end. /*for each ecm_mstr */

END PROCEDURE.
